<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客前台首页模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class IndexModel extends PublicModel{

	/**
	 * 处理首页日志分页
	 * @return [type] [description]
	 */
	Public function indexPost(){
		$C = $this->CheckIsOpen();
		if ($C['istop']) {
			$T = loadfile(C('top'));
			$Tcount = count($T);
			$count = 10 - $Tcount;
		} else {
			$count = 10;
		}
		$DATA = $this->getPost();
		$lastarr = array();
		for ($i=0; $i < count($DATA); $i++) {
			if ($i == $count) break;
			foreach ($DATA[$i] as $k => $v) {
				$lastarr[] = unfile(C('post.postdata').$k.'.Mark');
			}
		}
		return $this->getunFile($lastarr);
	}

	/**
	 * 置顶日志数据
	 * @return boolean [description]
	 */
	Public function isTop(){
		$C = $this->blogConfig();
		$top = loadfile(C('top'));
		uasort($top, 'Post_Sort');
		$lastarr = array();
		foreach ($top as $k => $v) {
			$lastarr[] = unfile(C('post.postdata').$k.'.Mark');
		}
		$redata['check'] = $this->CheckIsOpen();
		$redata['top'] = $this->getunFile($lastarr);
		return $redata;
	}

	/**
	 * 分析，取出上一篇，下一篇
	 * @param  [type] $pid [接收ID]
	 * @return [type]      [description]
	 */
	Public function postNextLast($pid){
		$post = parent::allPost();
		foreach ($post as $k => $v) {
			if ($v['isoff']) {
				if (strstr(strtolower($v['id']),strtolower($pid)) == true) {
					$id = $k;
				}
			}
		}
		if ($id == 0) {
			$lastp = '上一篇：没有了';
		} else {
			$lastp = '上一篇：<a href="'.U('index/showpost', array('post' => $post[$id-1]['id'])).'">'.$post[$id-1]['title'].'</a>';
		}
		if ($id == count($post)-1) {
			$nextp = '下一篇：没有了';
		} else {
			$nextp = '下一篇：<a href="'.U('index/showpost', array('post' => $post[$id+1]['id'])).'">'.$post[$id+1]['title'].'</a>';
		}
		$redata['lastp'] = $lastp;
		$redata['nextp'] = $nextp;
		return $redata;
	}

	/**
	 * 博客底部
	 * @return [type] [description]
	 */
	Public function indexfooter(){
		$C = parent::blogConfig();
		$titme = $C['runyear'].','.($C['runmon'] - 1).','.$C['runday'].','.$C['runhour'].','.$C['runmin'].','.$C['runsec'];
		$redata['title'] = $C['site_name'];
		$redata['time'] = $titme;
		$redata['copy'] = $C['copy_right'];
		return $redata;
	}

	/**
	 * 日志与专题点击次数
	 * @param [type] $id    [接收ID]
	 * @param [type] $where [接收需要日志或专题添加点击次数，识别号]
	 */
	Public function addHits($id, $where = null){
		$d = parent::fileDir();
		if ($where == 'post') {
			$filedir = $d['postdir'].$id.'.Mark';
		} elseif ($where == 'page') {
			$filedir = $d['pagedir'].$id.'.Mark';
		}
		if (!file_exists($filedir)) {
			return;
		}
		$data = unserialize(file_get_contents($filedir));
		$data['hits'] = !empty($data['hits']) ? $data['hits']+1 : 100;
		if (self::checkDataBase()) {
			file_put_contents($filedir, serialize($data));
		}
	}

	/**
	 * 检查日志与专题是否存在
	 * @return [type] [description]
	 */
	Public function checkDataBase(){
		$getpost = trim(G('get.post'));
		$getpid = trim(G('get.pid'));
		if (!empty($getpost)) {
			$P = parent::allPost();
			foreach ($P as $v) {
				if (strstr(strtolower($v['id']), strtolower(G('get.post')))) {
					return true;
				}
			}
		} elseif (!empty($getpid)) {
			$P = parent::indexPage();
			foreach ($P as $v) {
				if (strstr(strtolower($v['id']), strtolower(G('get.pid')))) {
					return true;
				}
			}
		}
	}

	/**
	 * 根据当前GET的ID号判断当前评论中的是日志还是专题，然后返回
	 * @param  [type] $id [当前ID号]
	 * @return [type]     [description]
	 */
	Public function checkUrl($id){
		$P = parent::allPost();
		foreach ($P as $v) {
			if (strstr(strtolower($id), strtolower($v['id'])) == true) {
				return true;
			}
		}
	}
}